<?php
namespace app\[MODULE]\controller[FLEX];
use think\Db;
use think\facade\Cache;
use think\facade\Request;
use app\[MODULE]\model[FLEX]\[UTABLE] as [UTABLE]Model;
use services\JsonService;
use services\UtilService as Util;
use think\facade\Route as Url;
use services\FormBuilder as Form;
use app\admin\controller\AuthController;

class [CONTROLLER] extends AuthController{
    

    /**
     * 首页
     */
    public function index(){
        if (Request::isAjax()) {
            $where = Util::getMore([
                ['page', 1],
                ['limit', 20]
            ]);
            return JsonService::successlayui([UTABLE]Model::getList($where));
        }
        return $this->fetch();

    }
    
    
    /**
     * 添加
     * 
     * @return mixed|\think\response\Json|void
     */
    public function create(){
        if(Request::isPost()){
            $postData  = Request::post();
            if([UTABLE]Model::create($postData)){
                return JsonService::successful('添加成功');
            }else{
                return JsonService::fail('添加失败');
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑
     * @param $id
     * @return mixed|\think\response\Json|void
     */
    public function edit($id){
        if(Request::isPost()){
            $postData  = Request::post();
            if([UTABLE]Model::update($postData)){
                return JsonService::successful('编辑成功');
            }else{
                return JsonService::fail('编辑失败');
            }
        }else{
             if(!$id) return JsonService::fail('缺少ID');
             if(!$info = [UTABLE]Model::get($id)) return JsonService::fail('无此信息');
             $this->assign('info',$info);
             return $this->fetch();
        }
    }


    /**
     * @name: 表格导出
     * @msg: 
     * @return {json} 
     */
    public function export(){
        $data = [UTABLE]Model::select();
        $data = count($data) ? $data->toArray() : [];
        $count= [UTABLE]Model::count();
        return JsonService::successlayui(compact('data','count'));
    }

    /**
     * 快速编辑
     *
     * @return json
     */
    public function set_value($field = '', $id = '', $value = '')
    {
        $field == '' || $id == '' || $value == '' && JsonService::fail('缺少参数');
        if ([UTABLE]Model::where(['id' => $id])->update([$field => $value])) {
            return JsonService::successful('保存成功');
        } else {
            return JsonService::fail('保存失败');
        }

    }

        /**
     * @name: 删除
     * @Author: Sam
     * @Date: 2020-05-21 16:37:42
     * @msg:
     * @param {type}
     * @return:
     */
    public function delete()
    {
        $id = Request::param('id/a'); //转化数组
        !$id && JsonService::fail('缺少参数');
        if ([UTABLE]Model::del($id)) {
            return JsonService::successful('删除成功');
        } else {
            return JsonService::fail('删除失败');
        }

    }

}